package com.lanqiao.companymanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanqiao.companymanage.entity.AssetClass;
import com.lanqiao.companymanage.entity.AssetInformation;
import com.lanqiao.companymanage.service.AssetMessService;
import com.lanqiao.companymanage.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 何小棍
 * 资产信息管理与添加
 */
@RestController
@RequestMapping("/assetMess")
@Api(tags = "资产信息管理")
public class AssetMessController {
    @Autowired
    AssetMessService assetMessService;

    /**
     * 分页查询
     */
    @GetMapping("/assetMessPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数,默认第一页"),
            @ApiImplicitParam(name = "size", value = "每页显示条数，默认 10")})
    @ApiOperation(value = "分页查询")
    public Object assetMessPage(Page<AssetInformation> assetMessPage) {
        Page<AssetInformation> page = assetMessService.listPage(assetMessPage);
        return Result.success(page);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/selectById")
    @ApiOperation(value = "根据id查询")
    public Object selectById(Integer id) {
        AssetInformation assetMess = assetMessService.selectById(id);
        if (assetMess == null) {
            return Result.err("找不到对应id的值");
        } else {
            return Result.success("查询成功", assetMess);
        }
    }

    /**
     * 资产信息管理
     *
     * @param assetMess
     * @return
     */
    @PostMapping("/addAssetMess")
    @ApiOperation(value = "资产信息添加")
    public Object addAssetMess(AssetInformation assetMess) {
        AssetClass assetClass = new AssetClass();
        assetMess.setAssetClass(assetClass);
        int i = assetMessService.addAssetMess(assetMess);
        if (i == 0) {
            return Result.err("添加失败");
        } else {
            return Result.success("添加成功", null);
        }
    }

    /**
     * 根据id删除
     */
    @GetMapping("/deletedById")
    @ApiOperation(value = "根据id删除")
    public Object deletedById(Integer id) {
        int i = assetMessService.deleteById(id);
        if (i == 0) {
            return Result.err("删除失败");
        } else {
            return Result.success("删除成功", null);
        }
    }

    /**
     * 根据id修改
     */
    @PostMapping("/updateAssetMess")
    @ApiOperation(value = "根据id修改")
    public Object updateAssetMess(AssetInformation assetMess) {
        int i = assetMessService.updateAssetMess(assetMess);
        if (i == 0) {
            return Result.err("修改失败");
        } else {
            return Result.success("修改成功", null);
        }
    }

    /**
     * 根据年终类型查询价值和数量
     *
     * @return
     */
    @RequestMapping("/showTableOne")
    public Map<String, String> showTableOne() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("addCount", String.valueOf(assetMessService.listMaps(new QueryWrapper<AssetInformation>().select("count(*) as addCount").eq("zjtype", "增加")).get(0).get("addCount")));
        hashMap.put("addSum", String.valueOf(assetMessService.listMaps(new QueryWrapper<AssetInformation>().select("sum(zimoney) as addSum").eq("zjtype", "增加")).get(0).get("addSum")));
        hashMap.put("reserveCount", String.valueOf(assetMessService.listMaps(new QueryWrapper<AssetInformation>().select("count(*) as reserveCount").eq("zjtype", "减少")).get(0).get("reserveCount")));
        hashMap.put("reserveSum", String.valueOf(assetMessService.listMaps(new QueryWrapper<AssetInformation>().select("sum(zimoney) as reserveSum").eq("zjtype", "减少")).get(0).get("reserveSum")));
        return hashMap;
    }
}
